package es.udc.empresauriostic.geofinder.view.utils;

import java.io.IOException;
import java.util.List;

import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.os.AsyncTask;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;
import es.udc.empresauriostic.geofinder.R;
import es.udc.empresauriostic.geofinder.model.GameUserData;
import es.udc.empresauriostic.geofinder.model.ServerAPI;

/**
 * View utils.
 * 
 * @author Santiago Munín González
 * 
 */
public class ViewUtils {
	/**
	 * Displays a connection error.
	 * 
	 * @param context
	 *            Application context.
	 */
	public static void showToastConnectionProblem(Context context) {
		Toast.makeText(context, R.string.connection_problem, Toast.LENGTH_SHORT)
				.show();
	}

	// TODO comment
	public static void showToast(Context context, int stringId) {
		Toast.makeText(context, stringId, Toast.LENGTH_SHORT).show();
	}

	// TODO comment
	public static Dialog getHighScoresDialog(Context context,
			GameUserData player, List<GameUserData> rivals) {
		AlertDialog.Builder builder = new AlertDialog.Builder(context);
		LayoutInflater inflater = LayoutInflater.from(context);
		View view = inflater.inflate(R.layout.scores_dialog, null);
		builder.setView(view)
				.setPositiveButton(android.R.string.ok,
						new DialogInterface.OnClickListener() {
							@Override
							public void onClick(DialogInterface dialog, int id) {
							}
						}).setTitle(R.string.top_scores);
		fillScores(view, player, rivals);

		return builder.show();
	}

	private static void fillScores(View view, GameUserData player,
			List<GameUserData> rivals) {
		int i = 0;
		for (GameUserData gameUserData : rivals) {
			if (!gameUserData.equals(player)) {
				fillRow(view, i++, gameUserData);
			}
		}
		// TODO parametizer
		for (int j = i; j < 5; j++) {
			fillRow(view, i++, null);
		}

		fillRow(view, 5, player);
	}

	private static void fillRow(View view, int rowPos, GameUserData userData) {
		TextView username = null, score = null;

		switch (rowPos) {
		case 0:
			username = ((TextView) view.findViewById(R.id.row_0_tv_0));
			score = ((TextView) view.findViewById(R.id.row_0_tv_1));
			break;
		case 1:
			username = ((TextView) view.findViewById(R.id.row_1_tv_0));
			score = ((TextView) view.findViewById(R.id.row_1_tv_1));
			break;
		case 2:
			username = ((TextView) view.findViewById(R.id.row_2_tv_0));
			score = ((TextView) view.findViewById(R.id.row_2_tv_1));
			break;
		case 3:
			username = ((TextView) view.findViewById(R.id.row_3_tv_0));
			score = ((TextView) view.findViewById(R.id.row_3_tv_1));
			break;
		case 4:
			username = ((TextView) view.findViewById(R.id.row_4_tv_0));
			score = ((TextView) view.findViewById(R.id.row_4_tv_1));
			break;
		case 5:
			username = ((TextView) view.findViewById(R.id.row_5_tv_0));
			score = ((TextView) view.findViewById(R.id.row_5_tv_1));
			break;
		}
		if (userData != null) {
			if (rowPos < 5) {
				username.setText(userData.getUserName());
			}
			score.setText(String.valueOf(userData.getScore()));

		} else {
			username.setText("");
			score.setText("");
		}
	}

	public static class AsyncScoresFetcher extends
			AsyncTask<Integer, Void, List<GameUserData>> {
		private Context context;

		public AsyncScoresFetcher(Context context) {
			this.context = context;
		}

		@Override
		protected List<GameUserData> doInBackground(Integer... params) {
			try {
				if (params[0] == -1) {
					return ServerAPI.getHighScores(context);
				} else {
					return null;
				}
			} catch (IOException e) {
				return null;
			}
		}

		@Override
		protected void onPostExecute(List<GameUserData> result) {
			super.onPostExecute(result);
			ViewUtils.getHighScoresDialog(context, result.get(0),
					result.subList(1, result.size()));
		}

	}

}
